package com.omega.framework.common.model;

import com.omega.framework.common.exception.BaseServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;

/**
 * 基础错误码定义
 *
 * @author Lv.
 * @date 2022/11/2 12:17
 */
@Getter
@AllArgsConstructor
public enum BaseResponseCode implements BaseServiceException {
    /*
     *定义
     */
    OK("0", "成功"),
    ONLY_THROW_MSG("10000", "%s"),
    INVALID_PARA("10001", "无效的参数"),
    PARA_NULL("10002", "参数[%s]不能为空"),
    RESOURCE_NOT_FOUND("404", "资源不存在"),
    UNKOWN_EXCEPTION("-1", "系统异常，请联系攻城狮！"),
    INSERT_EXCEPTION("10", "数据新增失败！"),
    INSERT_BATCH_EXCEPTION("11", "数据新增失败！"),
    UPDATE_EXCEPTION("20", "数据更新失败！"),
    DELETE_EXCEPTION("30", "数据删除失败！"),
    DISABLE_EXCEPTION("31", "使数据无效失败！"),
    SELECT_ONE_EXCEPTION("40", "数据获取失败！"),
    SELECT_EXCEPTION("41", "数据获取失败！"),
    SELECT_PAGINATION_EXCEPTION("42", "数据获取失败！"),
    ;

    private String code;
    private String message;


    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    @Override
    public void setMessage(String msg) {
        this.message = msg;
    }

    /**
     * 获取枚举值
     *
     * @param code 编码
     * @return 响应码
     */
    public static BaseResponseCode getInstance(String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        return Stream.of(values()).filter((tn) -> tn.getCode().equals(code)).findFirst().orElse(null);
    }

}
